﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using demo.DBHelper;
using iAgileFramework.DataTransCarrier;

namespace demo.BLL
{
    public class BLLBase
    {      
        #region 会话相关
        private Session Session { get; set; }

        /// <summary>
        /// 打开默认数据库（多个查询操作打开可以提高性能）
        /// </summary>
        protected void ConnectionOpen()
        {
            if (this.Session == null)
            {
                this.Session = SessionManager.GetSession();
            }

            if (this.Session.Connection.State != ConnectionState.Open)
            {
                this.Session.Connection.Open();
            }
        }

        /// <summary>
        /// 关闭默认数据库
        /// </summary>
        protected void ConnectionClose()
        {
            if (this.Session.Connection.State != ConnectionState.Closed)
            {
                this.Session.Connection.Close();
            }
        }

        /// <summary>
        /// 开启事务（默认数据库）
        /// </summary>
        protected void TranBegin()
        {
            if (this.Session == null)
            {
                this.Session = SessionManager.GetSession();
            }

            this.Session.Begin();
        }

        /// <summary>
        /// 提交事务（默认数据库）
        /// </summary>
        protected void TranCommit()
        {
            this.Session.Commit();
        }

        /// <summary>
        /// 回滚事务（默认数据库）
        /// </summary>
        protected void TranRollback()
        {
            this.Session.Rollback();
        }

        #endregion

        protected void Dispose()
        {
            this.Session.Dispose();
            GC.SuppressFinalize(this);
        }
    }
}
